#include "display.h"
#include "n32g030_i2c.h"

static u8  fac_us = 0;
static u16 fac_ms = 0;

void DelayInit(void)
{
	SysTick->CTRL&=0xfffffffb;
	fac_us=SystemCoreClock/8;		    
	fac_ms=(u16)fac_us*1000;
}								    

void delay_ms(u16 nms)
{	 		  	  
	u32 temp;		   
	SysTick->LOAD=(u32)nms*fac_ms;
	SysTick->VAL =0x00;
	SysTick->CTRL=0x01;
	do
	{
		temp=SysTick->CTRL;
	}
	while(temp&0x01&&!(temp&(1<<16)));//??????   
	SysTick->CTRL=0x00;       //?????
	SysTick->VAL =0X00;       //?????	  	    
}   

void delay_us(u32 nus)
{		
	u32 temp;	    	 
	SysTick->LOAD=nus*fac_us;
	SysTick->VAL=0x00;
	SysTick->CTRL=0x01;
	do
	{
		temp=SysTick->CTRL;
	}
	while(temp&0x01&&!(temp&(1<<16)));//??????   
	SysTick->CTRL=0x00;       //?????
	SysTick->VAL =0X00;       //?????	 
}

void TM1637_Delay_us(unsigned  int Nus)
{
#if 1
	for(;Nus>0;Nus--)
	{
	  __nop();
	}
#else
	delay_us(Nus);
#endif	
}

void TM1637_Init(void)
{		
	GPIO_InitType GPIO_InitStructure;
//		RCC_APB2PeriphClockCmd(SCLK_GPIO_CLK|DIN_GPIO_CLK, ENABLE);
//		RCC_APB2PeriphClockCmd(EN_GPIO_CLK, ENABLE);
		
	GPIO_InitStruct(&GPIO_InitStructure);
	GPIO_InitStructure.Pin = TM1637_CLK_PIN | TM1637_DIO_PIN;	
	GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;     
	GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_HIGH;
	GPIO_InitPeripheral(SCLK_GPIO_PORT, &GPIO_InitStructure);   
	GPIO_SetBits(SCLK_GPIO_PORT, TM1637_CLK_PIN);	//  SCLK
	GPIO_SetBits(DIN_GPIO_PORT, TM1637_DIO_PIN); //DIN
	
}

void TM1637_Start(void)
{
    TM1637_DIO_OUT(); 
    TM1637_CLK(1);
    TM1637_DIO(1);
    TM1637_Delay_us(2);
    TM1637_DIO(0);
    TM1637_Delay_us(2);
    TM1637_CLK(0);
}

void TM1637_Stop(void)
{
	TM1637_DIO_OUT(); 
	TM1637_CLK(0);
	TM1637_Delay_us(2);
	TM1637_DIO(0);
	TM1637_Delay_us(2);
	TM1637_CLK(1);
	TM1637_Delay_us(2);
	TM1637_DIO(1);
}

void TM1637_Ack(void)
{
    TM1637_CLK(0);
#if 0	
    TM1637_DIO_OUT(); 

		TM1637_DIO(0);
    TM1637_Delay_us(2);
#else
		TM1637_DIO_IN();
		while(TM1637_READ_DIO == 1);
#endif	
		TM1637_CLK(1);
    TM1637_Delay_us(2);
    TM1637_CLK(0);
}

void TM1637_WriteByte(unsigned char oneByte)
{
    unsigned char i;
    TM1637_DIO_OUT();
    
    for(i=0;i<8;i++)
    {
        TM1637_CLK(0);
        if(oneByte&0x01)
        {
            TM1637_DIO(1);
        }
        else
        {
        TM1637_DIO(0);
        }
        TM1637_Delay_us(3);
        oneByte = oneByte>>1;
        TM1637_CLK(1);
        TM1637_Delay_us(3);
    }
}

u8 idx = 0;

void TM1637_SendData(u8 Addr1640,u8 *a,u8 ValueLong)  
{  
		u8 i;       
		TM1637_Start();  
		TM1637_WriteByte(Addr1640);   
		TM1637_Ack();
		for(i=0;i<ValueLong;i++)  
		{  
			TM1637_WriteByte(a[i]);  
			TM1637_Ack();
		}        
		TM1637_Stop(); 
}

const uint8_t status_data[] =	
{
//	0xff,0xff,0xff,0xff,0xff,0xff,0xff,
    0x3F,/*0*/
    0x06,/*1*/
    0x5B,/*2*/
    0x4F,/*3*/
    0x66,/*4*/
    0x6D,/*5*/
    0x7D,/*6*/
    0x07,/*7*/
    0x7F,/*8*/
    0x6F,/*9*/
    0x77,/*10 A*/
    0x7C,/*11 b*/
    0x58,/*12 c*/
    0x5E,/*13 d*/
    0x79,/*14 E*/
    0x71,/*15 F*/
    0x76,/*16 H*/
    0x38,/*17 L*/
    0x54,/*18 n*/
    0x73,/*19 P*/
    0x3E,/*20 U*/
    NO_DISPLAY_DATA,/*21 ??*/
};

void	DisplayStatus(bool initial,bool LEDPowerOn,U8 * LedSts)
{
	if (initial)
	{
		DelayInit();
		TM1637_Init();
	}
	TM1637_Start();
	TM1637_WriteByte(0x40);
	TM1637_Ack();
	TM1637_Stop();
	if (LEDPowerOn)
	{
	//	TM1640Start();
		TM1637_SendData(0xc0,LedSts,LED_CNT);
	// 	TM1640Stop();
	}
	TM1637_Start();
	if (LEDPowerOn)
	{
		TM1637_WriteByte(0x8A);
	}
	else
	{
		TM1637_WriteByte(0x0A0);
	}
	TM1637_Ack();
	TM1637_Stop();
}

void DisplayErrorCode(U8 ErrorCode)
{
	U8 LED_Sts[LED_CNT];
	LED_Sts[0] = status_data[14];
	LED_Sts[1] = status_data[(ErrorCode & 0xf0) >> 4];
	LED_Sts[2] = status_data[ErrorCode & 0x0f];
	LED_Sts[3] = LED_Sts[4] = LED_Sts[5] = NO_DISPLAY_DATA;
	DisplayStatus(false,true,LED_Sts);	
}

void	UpdateLEDStatus(void)
{
	U8 LED_Sts[LED_CNT];
	U16 temp1,temp2;
	
	LED_Sts[0] = status_data[19];
	if (SettingPhase >= 9)
	{
		LED_Sts[1] = status_data[1];
		LED_Sts[2] = status_data[SettingPhase - 9];
	}
	else
	{
		LED_Sts[1] = status_data[SettingPhase + 1];
		LED_Sts[2] = status_data[21];	//No Display
	}
	temp1 = CurSetting.data[SettingPhase];
	temp2 = temp1 / 100;
	temp1 %= 100;
	LED_Sts[3] = 	status_data[temp2];
	temp2 = temp1 / 10;
	temp1 %= 10;
	LED_Sts[4] = 	status_data[temp2];
	LED_Sts[5] = 	status_data[temp1];
	DisplayStatus(false,true,LED_Sts);
}

